/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package retrieval;

import java.util.ArrayList;
import java.util.HashMap;

/**
 *
 * @author Danang
 */
public class SearchConsecutive {
    HashMap<String,SearchConsecutiveModul> searchList;
    ArrayList<String> tokens;
    int result=0;
    ArrayList<Integer> hasil;
    
    public SearchConsecutive() {
        searchList = new HashMap<String, SearchConsecutiveModul>();
        tokens = new ArrayList<String>();
        hasil = new ArrayList<Integer>();
    }
    
    public void addList(String token, ArrayList<Integer> list) {
        tokens.add(token);
        SearchConsecutiveModul s = new SearchConsecutiveModul(list);
        searchList.put(token, s);
    }
    
    public void doSearch() {
        boolean check = true;
        while (!stopIteration()) {
            int i = 0;
            while (i<tokens.size()-1) {
                int first = i;
                int second = i+1;
                if (!doTwoSearch(first, second)) {
                    check = false;
                    break;
                }
                i++;
            }
            if (check) {
                result++;
                String token = tokens.get(0);
                hasil.add(searchList.get(token).getPrevHead());
            }
            check = true;
        }
    }
    
    boolean doTwoSearch(int firstID, int secondID) {
        String token1 = tokens.get(firstID);
        String token2 = tokens.get(secondID);
        SearchConsecutiveModul first = searchList.get(token1);
        SearchConsecutiveModul second= searchList.get(token2);
        while (first.getHead()!=-1 && second.getHead()!=-1) {
            if (first.getHead()>=second.getHead()) {
                second.advNext(first.getHead());
                second.advHead();
            } else {
                first.advNext(second.getHead());
            }
            
            // checking
            if (first.getHead()+token1.length()+1==second.getHead()) {
                first.advHead();
                return true;
            }
            first.advHead();
        }
        return false;
    }
    
    boolean checkInLength() {
        for (int i=0;i<tokens.size()-1;i++) {
            String token1 = tokens.get(i);
            SearchConsecutiveModul sm1 = searchList.get(token1);
            String token2 = tokens.get(i+1);
            SearchConsecutiveModul sm2 = searchList.get(token2);
            if (sm1.getHead()+token1.length()+1!=sm2.getHead()) {
                return false;
            }
        }
        return true;
    }
    
    boolean stopIteration() {
        boolean stop = false;
        for (int i=0;i<tokens.size();i++) {
            String token = tokens.get(i);
            SearchConsecutiveModul sm = searchList.get(token);
            if (sm.getHead()==-1) {
                return true;
            }
        }
        return stop;
    }
    
    public int getCountResult() {
        return result;
    }
    
    public ArrayList<Integer> getListHasil() {
        return this.hasil;
    }
    
}
